package com.manxi.mall.controller;  // 修改包路径

import com.manxi.mall.bean.TrafficUvCt;  // 修改包路径
import com.manxi.mall.service.TrafficStatsService;  // 修改包路径
import com.manxi.mall.util.DateFormatUtil;  // 修改包路径
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;

/**
 * @author Addroc
 * @date 2024/6/14
 * 流量域统计Controller
 */
@RestController
@RequestMapping("/traffic")
public class TrafficStatsController {

    @Autowired
    private TrafficStatsService trafficStatsService;

    /**
     * 获取各渠道的UV（独立访客）数量/traffic/ch
     * @param date 日期参数，默认为0表示当天
     * @param limit 返回的渠道数量，默认为10
     * @return 返回各渠道UV数量的JSON格式数据
     */
    @RequestMapping("/ch")
    public String getChUvCt(
            @RequestParam(value = "date", defaultValue = "0") Integer date,
            @RequestParam(value = "limit", defaultValue = "10") Integer limit) {

        if (date == 0) {
            // 如果未传递日期参数，则使用当天日期
            date = DateFormatUtil.now();
        }
        List<TrafficUvCt> trafficUvCtList = trafficStatsService.getChUvCt(date, limit);
        List<String> chList = new ArrayList<>();
        List<Integer> uvCtList = new ArrayList<>();
        for (TrafficUvCt trafficUvCt : trafficUvCtList) {
            chList.add(trafficUvCt.getCh());
            uvCtList.add(trafficUvCt.getUvCt());
        }

        String json = "{\"status\": 0,\"data\":{\"categories\": [\"" + StringUtils.join(chList, "\",\"") + "\"],\n" +
                "    \"series\": [{\"name\": \"渠道\",\"data\": [" + StringUtils.join(uvCtList, ",") + "]}]}}";
        return json;
    }

    /**
     * 获取各地区的PV（页面浏览量）数量/traffic/regionPv
     * @param date 日期参数，默认为0表示当天
     * @return 返回各地区PV数量的JSON格式数据
     */
    @RequestMapping("/regionPv")
    public String getRegionPv(@RequestParam(value = "date", defaultValue = "0") Integer date) {
        if (date == 0) {
            date = DateFormatUtil.now();
        }
        List<TrafficUvCt> regionPvList = trafficStatsService.getRegionPv(date);
        List<String> regionList = new ArrayList<>();
        List<Integer> pvList = new ArrayList<>();
        for (TrafficUvCt trafficUvCt : regionPvList) {
            regionList.add(trafficUvCt.getCh());  // 假设Ch字段存储地区信息
            pvList.add(trafficUvCt.getUvCt());   // 假设UvCt字段存储PV数量
        }

        String json = "{\"status\": 0,\"data\":{\"categories\": [\"" + StringUtils.join(regionList, "\",\"") + "\"],\n" +
                "    \"series\": [{\"name\": \"地区\",\"data\": [" + StringUtils.join(pvList, ",") + "]}]}}";
        return json;
    }

    /**
     * 获取各设备的UV（独立访客）数量/traffic/deviceUv  
     * @param date 日期参数，默认为0表示当天
     * @return 返回各设备UV数量的JSON格式数据
     */
    @RequestMapping("/deviceUv")
    public String getDeviceUv(@RequestParam(value = "date", defaultValue = "0") Integer date) {
        if (date == 0) {
            date = DateFormatUtil.now();
        }
        List<TrafficUvCt> deviceUvList = trafficStatsService.getDeviceUv(date);
        List<String> deviceList = new ArrayList<>();
        List<Integer> uvList = new ArrayList<>();
        for (TrafficUvCt trafficUvCt : deviceUvList) {
            deviceList.add(trafficUvCt.getCh());  // 假设Ch字段存储设备信息
            uvList.add(trafficUvCt.getUvCt());   // 假设UvCt字段存储UV数量
        }

        String json = "{\"status\": 0,\"data\":{\"categories\": [\"" + StringUtils.join(deviceList, "\",\"") + "\"],\n" +
                "    \"series\": [{\"name\": \"设备\",\"data\": [" + StringUtils.join(uvList, ",") + "]}]}}";
        return json;
    }
}